<?php 

ini_set("memory_limit", "64M");
if(!isset($_SESSION)) 
{ 
session_start(); 
} 
include 'checkdate.php';
unset($_SESSION['mylistcarnum']);
unset($_SESSION['currentcarnum']);
//$s_date = $_REQUEST['s_date'];
 
if (isset($_REQUEST['datepickerfrom']) and isset($_REQUEST['datepickerto'])) {
	include '../connection/connection.php';
	set_time_limit(0);
	$winH = 350;
	if(isset($_REQUEST['winH'])) $winH = $_REQUEST['winH'];
	$m_fromdate = $_REQUEST['datepickerfrom'];
	$l_from = "";
	if ($_REQUEST['s_hourfrom'] != "" && is_int((int)$_REQUEST['s_hourfrom'])){
		$l_from = $_REQUEST['s_hourfrom'];
	}else{
		$l_from = "00";
	}
	if ($_REQUEST['s_munitefrom'] != "" && is_int((int)$_REQUEST['s_munitefrom'])){
		$l_from .= ":".$_REQUEST['s_munitefrom'];	
	}else{
		$l_from .= ":00";
	}
	if ($_REQUEST['s_secondfrom'] != "" && is_int((int)$_REQUEST['s_secondfrom'])){
		$l_from .= ":".$_REQUEST['s_secondfrom'];	
	}else{
		$l_from .= ":00";
	}
	
	$m_todate = $_REQUEST['datepickerto'];
	$l_to = "";
	if ($_REQUEST['s_hourto'] != "" && is_int((int)$_REQUEST['s_hourto'])){
		$l_to = $_REQUEST['s_hourto'];
	}else{
		$l_to = "00";
	}
	if ($_REQUEST['s_muniteto'] != "" && is_int((int)$_REQUEST['s_muniteto'])){
		$l_to .= ":".$_REQUEST['s_muniteto'];	
	}else{
		$l_to .= ":00";
	}
	if ($_REQUEST['s_secondto'] != "" && is_int((int)$_REQUEST['s_secondto'])){
		$l_to .= ":".$_REQUEST['s_secondto'];	
	}else{
		$l_to .= ":00";
	}
	
	$fromdate = $m_fromdate." ".$l_from;
	$todate = $m_todate." ".$l_to;
	$sql = "select t.id,c.name,t.car_datetime,t.carnumber,c.host,t.filename,t.isReg from carnum t  ,configcamera c where t.id=c.id and t.car_datetime>='" . $fromdate . "' and t.car_datetime<='" . $todate . "'";
	if (isset($_REQUEST['s_reg'])) {
	    if ($_REQUEST['s_reg'] == 0) {
	        $sql = $sql . " and t.isReg=0";
	    } else if ($_REQUEST['s_reg'] == 1) {
	        $sql = $sql . " and t.isReg=1";
	    }
	}
	if (isset($_REQUEST['s_cartype'])) {
	    if ($_REQUEST['s_cartype'] != 0) {
	        $cartype = $_REQUEST['s_cartype'];
	        $sql = $sql . " and car_type=" . $cartype;
	    }
	}
	
	if (isset($_REQUEST['s_carnum'])) {
	    $carnumber = $_REQUEST['s_carnum'];
	    if ($carnumber != "") {
	        $sql = $sql . " and t.carnumber like '%$carnumber%'";
	    }
	}
	$s = "";
	if (isset($_REQUEST['checkCamera'])) {

            foreach ($_REQUEST['checkCamera'] as $key => $value) {
                $s .=  $key . ",";
            }
            $s = substr($s, 0, -1);
            $s = '( ' . $s . ' )';
            $sql.=' and t.id in ' . $s;
       }
    //echo $sql;
    //return;
    $sql .= " order by car_datetime desc";
	$listSearch = mysql_query($sql,$connection);
	$querycartotal = mysql_query("select count(*) as c from carnum",$connection);
	$cartotal = 0;
	while ($row = mysql_fetch_array($querycartotal)) {
		$cartotal = $row[c];
	}
	
	$index = 0;
	$mylistcarnum = array();
	while ($row = mysql_fetch_array($listSearch)){
		$mylistcarnum[$index] = array($row['id'],$row['name'],$row['car_datetime'],$row['carnumber'],$row['host'],$row['filename'],$row['isReg']);
				
					 ?>
					 <tr class="trkhac" onclick = "SelectCar(this,'<?=$row['carnumber']?>','select<?=$index?>')" values="<?=$index?>">
					 	<td width="5%" align="center"><input onclick="SelectCarcheck('select<?=$index?>')" id="select<?=$index?>" type="checkbox" class="checkCarnum" name="checkCarnum[<?=$index?>]"/></td>
					 	<td width="5%" align="center"><?=$index?></td>
					 	<td width="43%"  align="center"><?=$row['name']?></td>
					 	<td width="25%"  align="center"><?=$row['car_datetime']?></td>
					 	<td class="last"  align="center"><?=$row['carnumber']?></td>
					 </tr>  
					 <?php 
					 $index+=1;
				
	}  
	if ($index>0 and $index<$winH/28) {
	 	foreach(range(0,$winH/28-$index) as $i){
	 		?>
	 		<tr>
		 	<td width="5%">&nbsp;</td>
		 	<td width="5%">&nbsp;</td>
		 	<td width="35%">&nbsp;</td>
		 	<td width="25%">&nbsp;</td>
		 	<td  class="last">&nbsp;</td>
		 	</tr>
	 	<?php }
	}
	?><input type="hidden" id="cartotal" value="<?=$index."/".$cartotal?>"/><?php
	
	$_SESSION['mylistcarnum'] = $mylistcarnum ;
	mysql_close($connection);
	  
}
?>

